package com.sto.stoapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.BaseActivity;

import sto.android.app.StoJNI;
import sto.android.app.StoPdaKeyEvent;

@SuppressLint("HandlerLeak")
public class StoImieActivity extends BaseActivity implements StoJNI.ScanCallBack {

	private final int max_length = 15;
	private EditText setImie;
	private Button saveBtn;
	// 扫描结果
	protected static final int SCAN_RESULT = 0x43;

	private StoJNI kaicomJNI;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == SCAN_RESULT) {
				if (msg.obj != null && !msg.obj.toString().equals("")) {
					String result = msg.obj.toString();
					setImie.setText(result);
					setImie.setSelection(setImie.getText().toString().length());
				} else {
					Helper.showSoundToast(StoImieActivity.this, "条码不能为空", false);
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sto_imie);

		initWidget();
		initListener();
		kaicomJNI = StoApplication.getStoJni();
		kaicomJNI.setmScanCB(this);
		kaicomJNI.SetScannerOn();
	}

	private void initWidget() {
		setImie = (EditText) findViewById(R.id.set_imie_edit);
		setImie.setInputType(InputType.TYPE_CLASS_NUMBER);
		saveBtn = (Button) findViewById(R.id.save);
	}

	private void initListener() {

		saveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String imie = setImie.getText().toString();
				if (imie.trim().length() == max_length) {
					imie = imie.trim();
					if (isDigit(imie)) {

						kaicomJNI.SetMachineCode(imie);
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						Helper.showToast(StoImieActivity.this,
								getString(R.string.set_success));
						Intent intent = new Intent();
						setResult(RESULT_OK, intent);
						finish();

					} else {
						Helper.showSoundToast(StoImieActivity.this, "唯一码必须是数字",
								false);
					}
				} else {
					Helper.showSoundToast(StoImieActivity.this, "设备唯一码必须是15位!",
							false);
				}
			}
		});
	}

	@Override
	public void onBackPressed() {

	}

	/**
	 * 监听按键事件
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		keyCode=StoApplication.getStoJni().getEventFuction(keyCode);
		try {
			if (event.getAction() == KeyEvent.ACTION_UP) {
				switch (keyCode) {
				case StoPdaKeyEvent.KEYCODE_SCAN:
					if (!scanStatus()) {
						startScan();
					}
					break;
				case StoPdaKeyEvent.KEYCODE_DEL:
					if (scanStatus()) {
						endScan();
					}
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ExceptionLog.writeLog("onKeyUp", e.getMessage());
		}
		return super.onKeyUp(keyCode, event);
	}

	/**
	 * 开始扫描
	 */
	public void startScan() {
		if (kaicomJNI != null) {
			kaicomJNI.SetScannerStart();
		}
	}

	/**
	 * 结束扫描
	 */
	public void endScan() {
		if (kaicomJNI != null) {
			kaicomJNI.SetScannerStop();
		}
	}

	/**
	 * 继续扫描
	 */
	public void scanContinue() {
		if (kaicomJNI != null) {
			kaicomJNI.SetScannerRetriger();
		}
	}

	/**
	 * 当前扫描状态
	 * 
	 * @return
	 */
	public boolean scanStatus() {
		if (kaicomJNI != null) {
			return kaicomJNI.GetScannerIsScanning();
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (kaicomJNI != null) {
			kaicomJNI.SetScannerOff();
		}
		handler.removeMessages(SCAN_RESULT);
	}

	/**
	 * 设置扫描超时时间
	 * 
	 * @param time
	 */
	public void setScanTimeOut(int time) {
		if (kaicomJNI != null) {
			kaicomJNI.SetScannerTimerOut(time);
		}
	}

	@Override
	public void onScanResults(String str) {
		Message msg = handler.obtainMessage();
		msg.what = SCAN_RESULT;
		msg.obj = str;
		handler.sendMessage(msg);
	}

	@Override
	public void onScanResults(String str, int type) {

	}

}
